package file.handle.impl;

import file.FileFrame;
import file.constant.PhotoEnum;
import file.constant.ViewByEnum;
import file.handle.FilePannel;
import file.listener.FilePanelListener;
import file.model.FileModel;
import file.model.FilePannelModel;
import file.model.ImageModel;
import file.untils.FileUntils;
import file.untils.PhotoUntils;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.util.ArrayList;

/**
 * 文件布局
 *
 * @Author: zou
 * @email: 704572528@qq.com
 * @Date: 2020/10/30 12:31
 */
public class FilePannelImpl implements FilePannel {

    private FileFrame fileFrame;

    public FilePannelImpl(FileFrame fileFrame) {
        this.fileFrame = fileFrame;
    }

    public static ImageIcon folderImage;
    public static ImageIcon fileImage;

    public static Boolean isChange = false;

    static {
        folderImage = new ImageIcon(ClassLoader.getSystemResource(PhotoUntils.getFolderSize(PhotoEnum.middle)));
        fileImage = new ImageIcon(ClassLoader.getSystemResource(PhotoUntils.getFileSize(PhotoEnum.middle)));
    }

    /**
     * 文件处理
     *
     * @param file
     * @param filePanel
     */
    @Override
    public void fileHandel(File file, JPanel filePanel) {

        if (file != null) {
            ArrayList<FileModel> fileModels = listPannelFileModel(file);
            ImageModel imageModel = new ImageModel(fileImage, folderImage);
            forFileModle(fileModels, filePanel, imageModel);
        }
    }

    /**
     * 添加fileModel
     *
     * @param fileModels
     * @param filePanel
     * @param imageModel
     */
    private void forFileModle(ArrayList<FileModel> fileModels, JPanel filePanel, ImageModel imageModel) {

        for (FileModel fileModel : fileModels) {
            this.addFilePanel(fileModel, filePanel, imageModel);
        }

    }

    /**
     * 获取文件处理
     *
     * @param file
     * @return
     */
    private ArrayList<FileModel> listPannelFileModel(File file) {
        File[] files = file.listFiles();
        ArrayList<FileModel> fdList = new ArrayList<>();
        if (files != null) {
            FileModel model;
            for (File file1 : files) {
                if (!file1.isHidden()) {
                    // 获取新文件对象
                    model = FileUntils.getFileModel(file1);
                    fdList.add(model);
                    // 添加文件
                    // this.addFilePanel(fileModel,filePanel);
                }
            }
        }
        return fdList;
    }

    /**
     * 增加 filePannel
     */
    @Override
    public void addFilePanel(FileModel fileModel, JPanel filePanel, ImageModel imageModel) {
        {
            /* filePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
             filePanel.setPreferredSize(new Dimension(600,900));*/
            //======== panel5 ========
            Panel panel55 = new Panel();
            {
                panel55.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 9));

                //======== panel6 ========
                Panel panel66 = new Panel();

                panel66.setLayout(new BorderLayout(10, 0));
                //---- label4 ----
                JLabel label44 = new JLabel();
                label44.setIcon(imageModel.getFileIcon());
                if (fileModel.getFolder()) {

                    label44.setIcon(imageModel.getFolderIcon());
                }
                panel66.add(label44, BorderLayout.WEST);

                //======== panel7 ========
                Panel panel77 = new Panel();

                JLabel label55 = new JLabel(fileModel.getName());
                label55.setPreferredSize(new Dimension(100, 17));
                JLabel label66 = new JLabel(fileModel.getType());
                JLabel label77 = new JLabel(fileModel.getSize());
                panel77.setLayout(new GridBagLayout());
                ((GridBagLayout) panel77.getLayout()).columnWidths = new int[]{0, 0};
                ((GridBagLayout) panel77.getLayout()).rowHeights = new int[]{0, 0, 0, 0};
                ((GridBagLayout) panel77.getLayout()).columnWeights = new double[]{0.0, 1.0E-4};
                ((GridBagLayout) panel77.getLayout()).rowWeights = new double[]{0.0, 0.0, 0.0, 1.0E-4};
                panel77.add(label55, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 5, 0), 0, 0));
                panel77.add(label66, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 5, 0), 0, 0));
                panel77.add(label77, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 0), 0, 0));

                panel66.add(panel77, BorderLayout.CENTER);

                panel55.add(panel66);
                // 添加监测器
                addListener(panel55, getFilePannelModel(fileModel, panel66, panel77, label55));
                addListener(panel66, getFilePannelModel(fileModel, panel66, panel77, label55));
                addListener(panel77, getFilePannelModel(fileModel, panel66, panel77, label55));
            }

            // jsp.revalidate();
            filePanel.add(panel55);
        }

    }

    /**
     * 改变文件布局
     *
     * @param vm
     * @param filePanel
     */
    @Override
    public void changeFilePanel(ViewByEnum vm, JPanel filePanel, String path) {

        if (filePanel == null) {
            return;
        }
        ImageIcon folderSmall;
        ImageIcon fileSmall;
        filePanel.removeAll();
        switch (vm) {
            case 平铺:
                // 默认布局
                this.fileHandel(new File(path), filePanel);
                isChange = false;
                break;
            case 大图标:
                folderSmall = new ImageIcon(ClassLoader.getSystemResource(PhotoUntils.getFolderSize(PhotoEnum.lager)));
                fileSmall = new ImageIcon(ClassLoader.getSystemResource(PhotoUntils.getFileSize(PhotoEnum.lager)));
                this.changePicture(filePanel, new File(path), new ImageModel(fileSmall, folderSmall));
                isChange = false;
                break;
            case 小图标:
                folderSmall = new ImageIcon(ClassLoader.getSystemResource(PhotoUntils.getFolderSize(PhotoEnum.small)));
                fileSmall = new ImageIcon(ClassLoader.getSystemResource(PhotoUntils.getFileSize(PhotoEnum.small)));
                this.changePicture(filePanel, new File(path), new ImageModel(fileSmall, folderSmall));
                isChange = false;
                break;
            case 详细信息:
                folderSmall = new ImageIcon(ClassLoader.getSystemResource(PhotoUntils.getFolderSize(PhotoEnum.small)));
                fileSmall = new ImageIcon(ClassLoader.getSystemResource(PhotoUntils.getFileSize(PhotoEnum.small)));
                this.addDetailpanel(new ImageModel(fileSmall, folderSmall), filePanel, path);
                this.updateUI(filePanel);
                break;
            case 中图标:
                folderSmall = new ImageIcon(ClassLoader.getSystemResource(PhotoUntils.getFolderSize(PhotoEnum.middle)));
                fileSmall = new ImageIcon(ClassLoader.getSystemResource(PhotoUntils.getFileSize(PhotoEnum.middle)));
                this.changePicture(filePanel, new File(path), new ImageModel(fileSmall, folderSmall));
                isChange = false;
                break;

        }
        filePanel.updateUI();

    }

    /**
     * 添加详细
     *
     * @param imageModel
     * @param filePanel
     * @param path
     */
    private void addDetailpanel(ImageModel imageModel, JPanel filePanel, String path) {
        File file = new File(path);
        ArrayList<FileModel> fileModels = listPannelFileModel(file);
        for (FileModel fm : fileModels) {
            this.addDetailPanel(fm, filePanel, imageModel);
        }
    }

    /**
     * 详细试图
     *
     * @param fm
     * @param filePanel
     * @param model
     */
    @Override
    public void detailPanel(FileModel fm, JPanel filePanel, ImageModel model) {
        // 添加
        this.addDetailPanel(fm, filePanel, model);

    }

    /**
     * 增加详细布局
     *
     * @param fm
     * @param filePanel
     * @param model
     */
    private void addDetailPanel(FileModel fm, JPanel filePanel, ImageModel model) {
        {
            Panel panel88 = new Panel();
            panel88.setLayout(new BorderLayout(5, 5));

            //======== panel15 ========
            Panel panel15 = new Panel();
            panel15.setLayout(new GridLayout(1, 3, 10, 10));

            if (!isChange) {

                JLabel label3 = new JLabel();
                //---- label3 ----
                label3.setText("名称");
                panel15.add(label3);

                JLabel label5 = new JLabel();
                //---- label5 ----
                label5.setText("修改日期");
                panel15.add(label5);

                //---- label4 ----
                JLabel label4 = new JLabel();
                label4.setText("类型");
                label4.setAlignmentY(0.0F);
                panel15.add(label4);

                //---- label18 ----
                JLabel label18 = new JLabel();
                label18.setText("大小");
                panel15.add(label18);
                panel88.add(panel15, BorderLayout.NORTH);
                isChange = true;
            }

            //======== detailPannel ========
            Panel panel = new Panel();
            panel.setLayout(new GridLayout(1, 4));

            //======== panel20 ========
            Panel panel20 = new Panel();
            panel20.setLayout(new BorderLayout(5, 5));

            //======== panel25 ========

            Panel panel25 = new Panel();
            panel25.setLayout(new BorderLayout(5, 5));

            //---- label13 ----
            JLabel label13 = new JLabel();
            label13.setText(fm.getName());
            panel25.add(label13, BorderLayout.CENTER);

            //---- label17 ----
            JLabel label17 = new JLabel();
            label17.setIcon(model.getFileIcon());
            if (fm.getFolder()) {
                label17.setIcon(model.getFolderIcon());
            }

            panel25.add(label17, BorderLayout.WEST);

            panel20.add(panel25, BorderLayout.WEST);
            panel.add(panel20);

            //---- label15 ----
            JLabel label15 = new JLabel();
            label15.setText(fm.getChangeDate());
            label15.setPreferredSize(new Dimension(200, 17));
            panel.add(label15);

            //---- label19 ----
            JLabel label19 = new JLabel();
            label19.setText(fm.getType());
            label19.setPreferredSize(new Dimension(200, 17));
            panel.add(label19);

            //---- label16 ----
            JLabel label16 = new JLabel();
            label16.setText(fm.getSize());
            label16.setPreferredSize(new Dimension(200, 17));
            panel.add(label16);
            panel88.add(panel, BorderLayout.CENTER);
            filePanel.add(panel88);
            addListener(panel88,getFilePannelModel(fm,panel88,panel,label13));
            addListener(panel15,getFilePannelModel(fm,panel15,panel,label13));
            addListener(panel20,getFilePannelModel(fm,panel20,panel,label13));
            addListener(panel25,getFilePannelModel(fm,panel25,panel,label13));
        }
    }

    /**
     * 改变查看方式
     *
     * @param filePanel
     * @param file
     * @param
     * @param
     */
    private void changePicture(JPanel filePanel, File file, ImageModel imageModel) {
        if (file != null) {
            ArrayList<FileModel> fileModels = this.listPannelFileModel(file);
            folderImage = imageModel.getFolderIcon();
            fileImage = imageModel.getFileIcon();
            // 添加到模块里
            forFileModle(fileModels, filePanel, imageModel);
        }
    }

    /**
     * 获取文件操作
     *
     * @param fileModel
     * @param panel66
     * @param panel77
     * @param label55
     * @return
     */
    private FilePannelModel getFilePannelModel(FileModel fileModel, Panel panel66, Panel panel77, JLabel label55) {
        FilePannelModel fpm = new FilePannelModel();
        fpm.setImagePannel(panel66);
        fpm.setTextPannel(panel77);
        fpm.setPathNameLable(fileFrame.getPathLable());
        fpm.setFileModel(fileModel);
        fpm.setFileNameLable(label55);
        return fpm;
    }

    /**
     * 添加监视器
     *
     * @param panel55
     * @param filePannelModel
     */
    private void addListener(Panel panel55, FilePannelModel filePannelModel) {
        panel55.addMouseListener(new FilePanelListener(fileFrame, filePannelModel));
    }


    @Override
    public void removeFile(JPanel filePanel) {
        filePanel.removeAll();
    }

    @Override
    public void updateUI(JPanel filePanel) {
        filePanel.updateUI();
        this.fileFrame.getScrollPane3().updateUI();
    }
}
